<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Model\Staff;
use App\Model\Time;
use App\Model\StaffCheck;
use App\Model\StaffLeave;
use App\Model\StaffSumary;
use App\User;
use App\Model\Trajectory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use DB;

class StaffController extends Controller
{
    /**
     * 签到信息
     *
     * @param Request $request
     * @return void
     */
    public function signin(Request $request) {

        $data = $request->all();
        $rules = [
            'image'  => 'required',
            'address'  => 'required',
            //'ramarks'=>'required'

        ];
        $message = [
                    'image.required'  => '照片不能为空',
                    'address.required'  => '地址不能为空',

                ];
        $validator = Validator::make($data,$rules,$message);
        if ($validator->fails()) {
            $error = $validator->errors()->first();
            return response()->json(['message' => $error,'code'=>400],200);
        }

        $user = Auth::user();
        $staff_check['user_id'] = $user->id;
        $staff_checkz['user_id'] = $user->id;
        $staff_check['image'] = $data['image'];
        $staff_check['address'] = $data['address'];
        $staff_check['created'] = time();
        $staff_checkz['created'] = time();
        $staff_check['day'] = date('Y-m-d',time());
        $staff_checkz['day'] = date('Y-m-d',time());
        $staff_check['remarks']=$data['remarks'];
        $one=$user->mwork;
        $two=$user->moffwork;
        $three=$user->awork;
        $four=$user->aoffwork;
        $a=strtotime(date($staff_check['day'].$one));
        $b=strtotime(date($staff_check['day'].$two));
        $c=strtotime(date($staff_check['day'].$three));
        $d=strtotime(date($staff_check['day'].$four));
        $e=strtotime(date($staff_check['day'].'13:00'));
        //条数
        $tiaoshu=StaffCheck::where('day',$staff_check['day'])->where('user_id',$staff_check['user_id'])->count();
        //上午时间段
        if($staff_check['created']<$a){
        	if($tiaoshu<1){
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=1;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else if($tiaoshu==1){
        		return response()->json(['message'=>'您已经上午签到成功了，请勿重复打卡','code' => 200], 200);
        	}
        }else if($staff_check['created']>$a && $staff_check['created']<$b){
        	if($tiaoshu<1){
        		//早上迟到打卡
        		$staff_check['status']=1;
        		$staff_check['zhuangtai']=1;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else if($tiaoshu==1){
        		//上午早退打卡
        		$staff_check['status']=2;
        		$staff_check['zhuangtai']=2;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else{
        		return response()->json(['message'=>'无效打卡2','code' => 400], 200);
        	}	
        }
        //中午上下班时间段
        if($staff_check['created']<$e && $staff_check['created']>=$b){
        	if($tiaoshu<1){
        		//早上缺卡中午下班打卡
        		$staff_checkz['status']=3;
        		$staff_checkz['zhuangtai']=1;
        		$queka = StaffCheck::insertGetId($staff_checkz);
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=2;
        		$id = StaffCheck::insertGetId($staff_check);
        			
        	}else if($tiaoshu==1){
        		//上午下班打卡
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=2;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else if($tiaoshu==2){
        		//上午下班打卡更新
        		$xid=StaffCheck::where(['day'=>$staff_check['day'],'zhuangtai'=>2])->first();
        		$id=$xid['id'];
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=2;
        		$zid=StaffCheck::where('id',$id )->update($staff_check);
        	}else{
        		return response()->json(['message'=>'无效打卡','code' => 400], 200);
        	}
        }else if($staff_check['created']<$c && $staff_check['created']>=$e){
        	if($tiaoshu<1){
        		//判断上午缺卡两条
        		$staff_checkz['status']=3;
        		$staff_checkz['zhuangtai']=1;
        		$queka = StaffCheck::insertGetId($staff_checkz);
        		$staff_checkz['zhuangtai']=2;
        		$quekaq = StaffCheck::insertGetId($staff_checkz);
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=3;
        		$id = StaffCheck::insertGetId($staff_check);
        			
        	}else if($tiaoshu==1){
        		//判断上午下班缺卡一条
        		$staff_checkz['status']=3;
        		$staff_checkz['zhuangtai']=2;
        		$queka = StaffCheck::insertGetId($staff_checkz);
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=3;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else if($tiaoshu==2){
        		//下午上班打卡
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=3;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else{
        		return response()->json(['message'=>'无效打卡','code' => 400], 200);
        	}
        }else if($staff_check['created']<$d && $staff_check['created']>=$c){
        	
        		//中午上班迟到打卡及早退打卡
        		if($tiaoshu==0){
        			//判断上午缺卡两条
        			$staff_checkz['status']=3;
        			$staff_checkz['zhuangtai']=1;
        			$queka = StaffCheck::insertGetId($staff_checkz);
        			$staff_checkz['zhuangtai']=2;
        			$quekaq = StaffCheck::insertGetId($staff_checkz);
        			$staff_check['status']=1;
        			$staff_check['zhuangtai']=3;
        			$id = StaffCheck::insertGetId($staff_check);	
        		}else if($tiaoshu==1){
        			//判断上午下班缺卡一条
        			$staff_checkz['status']=3;
        			$staff_checkz['zhuangtai']=2;
        			$queka = StaffCheck::insertGetId($staff_checkz);
        			$staff_check['status']=1;
        			$staff_check['zhuangtai']=3;
        			$id = StaffCheck::insertGetId($staff_check);
        		}else if($tiaoshu==2){
        			//下午迟到打卡
        			$staff_check['status']=1;
        			$staff_check['zhuangtai']=3;
        			$id = StaffCheck::insertGetId($staff_check);
        		}else if($tiaoshu==3){
        			//下午早退打卡
        			$staff_check['status']=2;
        			$staff_check['zhuangtai']=4;
        			$id = StaffCheck::insertGetId($staff_check);
        		}else{
        			return response()->json(['message'=>'无效打卡','code' => 400], 200);
        		}
        }
        //下午下班时间段
        if($staff_check['created']>$d){
        	if($tiaoshu<1){
        		$staff_checkz['status']=3;
        		$staff_checkz['zhuangtai']=1;
        		$queka = StaffCheck::insertGetId($staff_checkz);
        		$staff_checkz['zhuangtai']=2;
        		$quekaz = StaffCheck::insertGetId($staff_checkz);
        		$staff_checkz['zhuangtai']=3;
        		$quekaq = StaffCheck::insertGetId($staff_checkz);
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=4;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else if($tiaoshu==4){
        		$xid=StaffCheck::where(['day'=>$staff_check['day'],'zhuangtai'=>4])->first();
        		$id=$xid['id'];
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=4;
        		$zid=StaffCheck::where('id',$id )->update($staff_check);
        	}else if($tiaoshu==2){
        		$staff_checkz['status']=3;
        		$staff_checkz['zhuangtai']=3;
        		$quekaq = StaffCheck::insertGetId($staff_checkz);
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=4;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else if($tiaoshu==1){
        		$staff_checkz['status']=3;
        		$staff_checkz['zhuangtai']=2;
        		$quekaz = StaffCheck::insertGetId($staff_checkz);
        		$staff_checkz['zhuangtai']=3;
        		$quekaq = StaffCheck::insertGetId($staff_checkz);
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=4;
        		$id = StaffCheck::insertGetId($staff_check);
        	}else{
        		$staff_check['status']=0;
        		$staff_check['zhuangtai']=4;
        		$id = StaffCheck::insertGetId($staff_check);
        	}
        }
        $da=StaffCheck::where('id',$id)->first();
        if($da->zhuangtai==1){
        	$da->zhuangtai='上午上班';
        }else if($da->zhuangtai==2){
        	$da->zhuangtai='上午下班';
        }else if($da->zhuangtai==3){
        	$da->zhuangtai='下午上班';
        }else if($da->zhuangtai==4){
        	$da->zhuangtai='下午下班';
        }
        if($da->status==0){
        	$da->status='正常';
        }else if($da->status==1){
        	$da->status='迟到';
        }else if($da->status==2){
        	$da->status='早退';
        }
        $da->user_id=$user->name;
        $da->created=date('Y-m-d H:i:s',$da->created);
        if($id) {
            return response()->json(['data' => $da,'message'=>'签到成功','code' => 200], 200);
	    } else {
	        return response()->json(['data' => '','message'=>'签到失败','code' => 400], 200);
	    }
  }
 

    /**
     * 签到列表
     *
     * @return void
     */
    public function signin_list()
    {
        $user = Auth::user();
        $user_id = $user->id;
        $list = StaffCheck::where('user_id',$user_id)->select('id','image','address','created','status','zhuangtai')->orderby('id','desc')->get()->toarray();
        foreach($list as &$v) {
            $v['username'] = $user->name;
            $v['created'] = date('Y-m-d H:i:s',$v['created']);
            
	        if($v['zhuangtai']==1){
	        	$v['zhuangtai']='上午上班';
	        }else if($v['zhuangtai']==2){
	        	$v['zhuangtai']='上午下班';
	        }else if($v['zhuangtai']==3){
	        	$v['zhuangtai']='下午上班';
	        }else if($v['zhuangtai']==4){
	        	$v['zhuangtai']='下午下班';
	        }
	        if($v['status']==0){
	        	$v['status']='正常';
	        }else if($v['status']==1){
	        	$v['status']='迟到';
	        }else if($v['status']==2){
	        	$v['status']='早退';
	        }else if($v['status']==3){
	        	$v['status']='缺卡';
	        	$v['created'] = '无';
	        }
        }
        return response()->json(['data' => $list,'message'=>'查询成功','code' => 200], 200);
    }
     /**
     * 行为轨迹
     *
     * 
     */
    public function trajectory(Request $request)
    {
    	$user = Auth::user();
        $user_id = $user->id;
    	$data = $request->all();
    	$trajectory['staffid']=$user_id;
    	$trajectory['trajectory']=$data['trajectory'];
    	$trajectory['time']=time();
    	$id=Trajectory::insertGetId($trajectory);
    	if($id){
    		return response()->json(['message' =>'记录成功','code' => 200], 200);
    	}else{
    		return response()->json(['message' =>'记录失败','code' => 400], 200);
    	}
    	
        
    }
     /**
     * 请假
     *
     * 
     */
    public function leave(Request $request)
    {
		$data = $request->all();
        $rules = [
            'starttime'  => 'required',
            'endtime'  => 'required',
            'reason'=>'required'

        ];
        $message = [
                    'starttime.required'  => '起始时间不能为空',
                    'endtime.required'  => '结束时间不能为空',
					'reason.required'  => '请假理由不能为空',
                ];
        $validator = Validator::make($data,$rules,$message);
        if ($validator->fails()) {
            $error = $validator->errors()->first();
            return response()->json(['message' => $error,'code'=>400],200);
        }
		$user = Auth::user();
        $leave['userid'] = $user->id;
        $leave['starttime'] = $request->input('starttime');
		$leave['endtime'] = $request->input('endtime');
		$leave['reason'] = $request->input('reason');
		$leave['status']=0;
		$id=StaffLeave::insertGetId($leave);
    	if($id){
    		return response()->json(['message' =>'请假成功','code' => 200], 200);
    	}else{
    		return response()->json(['message' =>'请假失败','code' => 400], 200);
    	}
    }
    
    
    /**
     * 请假查看
     *
     * 
     */
    public function getleave(Request $request)
    {
    	$user = Auth::user();
        $userid = $user->id;
        $a=StaffLeave::where('userid', $userid)->orderby('id','desc')->get()->toarray();
        foreach($a as $k=>$v) {
        	$y=array();
        	$y['starttime']=$v['starttime'];
        	$y['endtime']=$v['endtime'];
        	$y['reason']=$v['reason'];
        	$y['status']=$v['status'];
        	if($v['ruserid']){
        		$b = User::where('id', $v['ruserid'])->first();
        		$y['rejectman']=$b->name;
        		$y['rejecttime']=$v['updated_at'];
        		$y['reject']=$v['reject'];
        	}
        	$z[]=$y;
        }
    	if($a){
    		return response()->json(['data'=>$z,'message' =>'查询成功','code' => 200], 200);
    	}else{
    		return response()->json(['message' =>'未请假','code' => 400], 200);
    	}
    }
    
     /**
     * 日总结
     *
     * 
     */
    public function summary(Request $request)
    {
		$data = $request->all();
        $rules = [
            'summary'  => 'required',
        ];
        $message = [
                    'summary.required'  => '总结内容不能为空',
                    
                ];
        $validator = Validator::make($data,$rules,$message);
        if ($validator->fails()) {
            $error = $validator->errors()->first();
            return response()->json(['message' => $error,'code'=>400],200);
        }
		$user = Auth::user();
        $summary['userid'] = $user->id;
        $summary['time']=time();
        $summary['summary'] = $request->input('summary');
        $start=strtotime(date('Y-m-d',time()));
        $end=strtotime(date('Y-m-d', strtotime("+1 day", $start)));
        
        $sum=StaffSumary::where('userid',$summary['userid'])->whereBetween('time',[$start,$end])->first();
		if($sum){
			return response()->json(['message' =>'已提交今日总结','code' => 200], 200);
		}
		$id=StaffSumary::insertGetId($summary);
    	if($id){
    		return response()->json(['data'=>$id,'message' =>'记录成功','code' => 200], 200);
    	}else{
    		return response()->json(['message' =>'记录失败','code' => 400], 200);
    	}
    }
    
    /**
     * 修改日总结
     *
     * 
     */
    public function upsummary(Request $request)
    {
		$data = $request->all();
        $rules = [
            'summary'  => 'required',
        ];
        $message = [
                    'summary.required'  => '总结内容不能为空',
                    
                ];
        $validator = Validator::make($data,$rules,$message);
        if ($validator->fails()) {
            $error = $validator->errors()->first();
            return response()->json(['message' => $error,'code'=>400],200);
        }
        
		$user = Auth::user();
        $userid = $user->id;
        $summary['summary'] = $request->input('summary');
        $summary['id'] = $request->input('id');
        $start=strtotime(date('Y-m-d',time()));
        $end=strtotime(date('Y-m-d', strtotime("+1 day", $start)));
        $id=StaffSumary::where('id', $summary['id'])->whereBetween('time',[$start,$end])->update($summary);
	    	if($id){
	    		return response()->json(['message' =>'修改成功','code' => 200], 200);
	    	}else{
	    		return response()->json(['message' =>'只能修改本日总结','code' => 400], 200);
    		} 
		
    }
    
    /**
     * 获取日总结
     *
     * 
     */
    public function getsummary(Request $request)
    {
    	$user = Auth::user();
        $userid = $user->id;
        $a=StaffSumary::where('userid', $userid)->orderby('id','desc')->get()->toarray();
    	if($a){
    		return response()->json(['data'=>$a,'message' =>'查询成功','code' => 200], 200);
    	}else{
    		return response()->json(['message' =>'还未提交今日总结','code' => 400], 200);
    	}
    }
   
}